package base_demo.test3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    static int ticket = 0;
    //static Object obj = new Object();
    static Lock lock = new ReentrantLock();//之呢个公用一把锁
    //每次关锁执行完后必须开锁
    @Override
    public void run() {
        while(true){
            lock.lock();
//           synchronized(MyThread.class){
            try {
                if(ticket < 100){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "正在售卖第" + ticket + "张票");
                } else {
                    break;
                }
            } catch (RuntimeException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }

        }
        }
    }
//}
